import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: localStorage.getItem('token') || null,
    user: null,
    role: '',
  }),
  actions: {
    login(credentials) {
      console.log('Logging in with:===>', credentials);
      return new Promise((resolve, reject) => {
        // 模拟登录请求
        setTimeout(() => {
          if (credentials.username&& credentials.password) {
            this.token = 'your_token_here';
            this.user = { name: 'Admin' };
            this.role = credentials.username == '管理端' ? '管理端' : (credentials.username == '单位端' ? '单位端' : '主管单位端')
            localStorage.setItem('token', this.token);
            localStorage.setItem('user', this.user);
            localStorage.setItem('role', this.role);
            resolve();
          } else {
            reject(new Error('登录失败'));
          }
        }, 100);
      });
    },
    logout() {
      this.token = null;
      this.user = null;
      localStorage.removeItem('token');
    }
  }
});